<template>
  <div>
    <el-container>
      <el-header height="70px">
        <p class="header_title">账号列表</p>
        <!--  -->
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>权限维护</el-breadcrumb-item>
          <el-breadcrumb-item>账户管理</el-breadcrumb-item>
          <el-breadcrumb-item>账户列表</el-breadcrumb-item>
          <el-breadcrumb-item></el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>

      <el-main>
        <el-card class="box-card">
          <div slot="header">
            <span>账号列表</span>
            <span style="float: right;">
              <i class="el-icon-arrow-up"></i>
              <i class="el-icon-close"></i>
            </span>
          </div>
          <!--  -->
          <el-row :gutter="20">
            <el-col :span="12">
              <el-input placeholder="根据名称查询" clearable>
                <el-button slot="append" icon="el-icon-search"></el-button>
              </el-input>
            </el-col>
            <el-col :span="4">
              <el-button type="primary" @click="addDialogVisible = true"
                >添加账号</el-button
              >
            </el-col>
            <el-col :span="5">
              <b class="sum_data">数据总数：</b>
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.pagenum"
                :page-size="pagination.pagesize"
                :page-sizes="[3, 5, 10, 20]"
                layout="total, sizes"
                :total="pagination.total"
                class="top_nav"
              >
              </el-pagination>
            </el-col>
          </el-row>

          <!-- 表格 -->
          <el-table :data="userlist" style="width: 100%" border stripe>
            <el-table-column width="60px" type="expand">
              <template slot-scope="props">
                <p class="roleTitle">所属角色列表</p>
                <el-form label-position="left" inline>
                  <el-form-item label="名称：">
                    <span>{{ props.row.rolename }}</span>
                  </el-form-item>
                </el-form>
              </template>
            </el-table-column>
            <el-table-column
              prop="username"
              label="名称"
              width="160px"
            ></el-table-column>
            <el-table-column
              prop="loginname"
              label="登录名"
              width="120px"
            ></el-table-column>
            <el-table-column prop="image" label="头像" width="65px">
              <template slot-scope="scope">
                <el-image
                  :src="scope.row.image"
                  style="width:40px;height:40px"
                  fit="cover"
                ></el-image>
              </template>
             
            </el-table-column>
            <el-table-column
              prop="date"
              label="创建时间"
              width="260px"
            ></el-table-column>
            <el-table-column prop="operate" label="操作">
              <template slot-scope="scope">
                <el-button
                  type="warning"
                  size="mini"
                  @click="showEditDialog(scope.row)"
                  >修改密码</el-button
                >
                <el-button type="primary" size="mini" @click="editAccount"
                  >编辑</el-button
                >
                <el-button type="danger" size="mini" @click="removeId(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pagination.pagenum"
            :page-sizes="[3, 5, 10, 15]"
            :page-size="pagination.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pagination.total"
          >
          </el-pagination>
        </el-card>
      </el-main>
    </el-container>

    <!-- 添加账号 -->
    <el-dialog
      title="添加账号"
      :visible.sync="addDialogVisible"
      width="60%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        lwidth="60%"
        class="demo-ruleForm"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="addForm.name" type="text"></el-input>
        </el-form-item>
        <el-form-item label="登录名" prop="loginname">
          <el-input v-model="addForm.loginname" type="text"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" type="password"></el-input>
        </el-form-item>
        <!-- <el-form-item label="确认密码" prop="password">
              <el-input v-model="addForm.password" placeholder="请重新输入密码" ></el-input>
            </el-form-item> -->

        <el-form-item prop="file">
          <div class="pictitle">
            <b>上传头像</b>(请上传文件大小不超过1MB的正方形图片)
          </div>
          <el-upload
            class="avatar-uploader"
            ref="upload"
            action="#"
            :show-file-list="false"
            :multiple="false"
            :auto-upload="false"
            :on-change="imgSaveToUrl"
          >
            <img v-if="addForm.file" :src="addForm.file" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑密码 -->
    <el-dialog
      title="修改行为信息"
      :visible.sync="editDialogVisible"
      width="60%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editlist"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="密码" prop="password">
          <el-input v-model="editlist.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editPassword">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {
        users: "",
        total: 2,
      },
      userlist: [],
      pagination: {
        pagenum: 1,
        pagesize: 3,
        total: 2,
      },
      pagelist: [],
      addDialogVisible: false,
      addForm: {
        username: "",
        images: "",
        loginname: "",
        password: "",
        phone: "",
        file: "",
      },
      addFormRules: {
        password: [{ required: true, message: "请选择密码", trigger: "blur" }],
      },
      //
      editDialogVisible: false,
      // 查询用户信息对象
      editForm: {
        loginname: "",
        username: "",
        password: "",
        phone: "",
        id: "",
      },
      // 修改表单的校验规则对象
      editFormRules: {
        loginname: [
          { required: true, message: "请输入登录名", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请更换密码", trigger: "blur" }],
        phone: [
          {
            required: true,
            min: 11,
            max: 11,
            message: "请输入手机号码",
            trigger: "blur",
          },
        ],
      },
      editlist: [],
    };
  },
  methods: {
    async getAccountList() {
      const res = await this.http.get("user/select", this.pagination);

      this.userlist = res.users;
      this.pagination.total = res.total;
      console.log(res);
    },
    handleSizeChange(newSize) {
      this.pagination.pagesize = newSize;
      this.getAccountList();
    },
    handleCurrentChange(newPage) {
      this.pagination.pagenum = newPage;
      this.getAccountList();
    },
    editAccount() {
      this.$router.push("/edit_account");
    },
    //图片上传
    async imgSaveToUrl(event) {
      if (this.beforeUploadPicture(event.raw)) {
        var formData = new FormData();
        // file为参数名
        formData.append("file", event.raw);
        console.log(formData);
        this.http.upload("/chart/add", formData).then((res) => {
          if (res.code == 1) {
            this.addForm.file = res;
            console.log(res);
            this.$message.success("上传成功");
          }
        });
      }
    },
    // 图片上传之前
    beforeUploadPicture(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt10M = file.size / 1024 / 1024 < 10;

      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG 或者 PNG 格式!");
      }
      if (isJPG && isPNG) {
        if (!isLt10M) {
          this.$message.error("上传头像图片大小不能超过 10MB!");
        }
      }

      return (isJPG || isPNG) && isLt10M;
    },
    // /chart/add
    addChart() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起添加网络请求
        const { res } = await this.http.post(" /chart/add", this.addForm);
        console.log(res);
        this.addDialogVisible = false;
        this.chartList();
      });
    },
    //
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    onSubmit() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        // 发起添加网络请求
        const { res } = await this.http.post("/user/add", this.addForm);

        if (status !== 400) {
          this.$message.error("添加行为失败");
        }
        this.$message.success("添加行为成功");
        this.addDialogVisible = false;
        this.getAccountList();
      });
    },
    //编辑行为
    async showEditDialog(row) {
      const res = await this.http.post("/user/updatePass", this.editForm);

      this.editlist = row;
      this.editDialogVisible = true;
    },
    // 监听编辑对话框关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    // 是否完成编辑行为信息
    editPassword() {
      this.$refs.editFormRef.validate(async (valid) => {
        console.log(valid);
        if (!valid) return;
        // 发起修改信息的信息的数据请求
        const res = await this.http.post("/user/updatePass", this.editlist, {
          name: this.editlist.name,
        });

        if (status !== 400) {
          this.$message.error("编辑行为信息失败");
        }
        console.log(res);
        // 关闭对话框
        this.editDialogVisible = false;
        // 刷新数据列表
        this.getAccountList();
        // 提交修改成功
        this.$message.success("更新行为信息成功");
      });
    },
    async removeId(row) {
      console.log(row);
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((error) => error);
      console.log(confirmResult);
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

      const res = await this.http.post("/user/delete",row.id);
      console.log(res);
      this.getAccountList();
    },
  },
  created() {
    this.getAccountList();
  },
};
</script>

<style scoped>
.roleTitle {
  font-weight: bold;
  padding-bottom: 5px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  border: 2px dashed #dedede;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-form-item__label {
  width: 100px;
}
</style>
